/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exterminator.states;

import com.jme3.app.state.AbstractAppState;
import com.jme3.math.Vector3f;
import exterminator.Main;
import exterminator.utils.LevelLoader.LevelInfo;
import tonegod.gui.core.Screen;
import tonegod.gui.framework.animation.Interpolation;
import tonegod.gui.framework.core.util.GameTimer;

/**
 *
 * @author t0neg0d
 */
public class CamTransitions extends AbstractAppState {
	Main main;
	Screen screen;
	
	Vector3f[] deckPositions = new Vector3f[4];
	Vector3f camPosition = new Vector3f();
	GameTimer transitionCamera;
	
	int currentDeck = 0;
	int nextDeck = 0;
	
	public CamTransitions(Main main) {
		this.main = main;
		this.screen = main.getScreen();
		
		initPositions();
		initTransition();
		
		main.getCamera().setLocation(deckPositions[currentDeck]);
	}
	
	private void initPositions() {
		deckPositions = new Vector3f[main.getLevelLoader().getLevelCount()];
		
		int index = 0;
		for (LevelInfo level : main.getLevelLoader().getCurrentScene().levels) {
			deckPositions[index] = level.camPosition;
			index++;
		}
	}
	
	public void nextDeck() {
		if (!screen.getAnimManager().hasGameTimer(transitionCamera)) {
			nextDeck = currentDeck + 1;
			if (nextDeck > (deckPositions.length-1))
				nextDeck = 0;
			startTransition();
		}
	}
	
	public void prevDeck() {
		if (!screen.getAnimManager().hasGameTimer(transitionCamera)) {
			nextDeck = currentDeck - 1;
			if (nextDeck < 0)
				nextDeck = (deckPositions.length-1);
			startTransition();
		}
	}
	
	public void moveToDeck(int index) {
		if (!screen.getAnimManager().hasGameTimer(transitionCamera)) {
			nextDeck = index;
			startTransition();
		}
	}
	
	private void initTransition() {
		transitionCamera = new GameTimer(0.75f) {
			@Override
			public void onComplete(float f) {
				finishTransition();
			}
		};
		transitionCamera.setInterpolation(Interpolation.exp5Out);
	}
	
	private void startTransition() {
		transitionCamera.reset(false);
		screen.getAnimManager().addGameTimer(transitionCamera);
	}
	
	private void finishTransition() {
		currentDeck = nextDeck;
	}
	
	@Override
	public void update(float tpf) {
		if (screen.getAnimManager().hasGameTimer(transitionCamera)) {
			camPosition.interpolate(
				deckPositions[currentDeck],
				deckPositions[nextDeck],
				transitionCamera.getPercentComplete()
			);
			main.getCamera().setLocation(camPosition);
		}
	}
}
